package com.lovely602.langchain4j.rag.controller;


import com.lovely602.langchain4j.rag.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping("/rag")
public class RAGController {

    @Resource(name = "inMemoryEmbeddingStore")
    private InMemoryEmbeddingStore<TextSegment> embeddingStore;

    @Resource(name = "chatAssistant")
    private ChatAssistant chatAssistant;

    /**
     * 测试rag调用
     */
    @GetMapping(value = "/test")
    public String test() throws FileNotFoundException {
        Document document = FileSystemDocumentLoader.loadDocument("E:\\audio\\秘密花园.doc");

//        FileInputStream fileInputStream = new FileInputStream("E:\\audio\\秘密花园.doc");
//        Document document = new ApacheTikaDocumentParser().parse(fileInputStream);

        EmbeddingStoreIngestor.ingest(document, embeddingStore);

        String result = chatAssistant.chat("我的目标是什么");

        System.out.println(result);

        return result;
    }
}
